package com.andnnl.pool;

import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.IntConsumer;
import java.util.function.IntSupplier;

/**
 * int参数改变，并回调
 * Created by chenss on 2021/12/30.
 */
public class IntParam {
    private int value;//获取原参数
    private IntSupplier newValueFunc;//获取新参数函数
    private IntConsumer changeFunc;//改变为新参数函数
    private BiConsumer<Integer, Integer> onChangeFunc;//获取线程池大小变化回调函数

    /**
     * 检查参数是否有变化
     */
    public void check() {
        Objects.requireNonNull(newValueFunc);
        Objects.requireNonNull(changeFunc);
        int newThreadSize = newValueFunc.getAsInt();//获取新值
        int oldThreadSize = value;//获取旧值
        if (newThreadSize != value) {
            value = newThreadSize;
            changeFunc.accept(newThreadSize);//改变值
            if (onChangeFunc != null)
                onChangeFunc.accept(newThreadSize, oldThreadSize);
        }
    }

    public IntParam() {
    }

    public IntParam(int value) {
        this.value = value;
    }

    public IntParam(int value, IntSupplier newValueFunc, IntConsumer changeFunc) {
        Objects.requireNonNull(newValueFunc);
        Objects.requireNonNull(changeFunc);
        this.value = value;
        this.newValueFunc = newValueFunc;
        this.changeFunc = changeFunc;
    }

    public IntParam(int value, IntSupplier newValueFunc, IntConsumer changeFunc, BiConsumer<Integer, Integer> onChangeFunc) {
        this(value, newValueFunc, changeFunc);
        this.onChangeFunc = onChangeFunc;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public IntSupplier getNewValueFunc() {
        return newValueFunc;
    }

    public void setNewValueFunc(IntSupplier newValueFunc) {
        this.newValueFunc = newValueFunc;
    }

    public BiConsumer<Integer, Integer> getOnChangeFunc() {
        return onChangeFunc;
    }

    public void setOnChangeFunc(BiConsumer<Integer, Integer> onChangeFunc) {
        this.onChangeFunc = onChangeFunc;
    }

    public IntConsumer getChangeFunc() {
        return changeFunc;
    }

    public void setChangeFunc(IntConsumer changeFunc) {
        this.changeFunc = changeFunc;
    }
}
